package com.cy.pj.sys.service.impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.pojo.PageObject;
import com.cy.pj.sys.dao.SysRoleDao;
import com.cy.pj.sys.dao.SysRoleMenuDao;
import com.cy.pj.sys.entity.CheckBox;
import com.cy.pj.sys.entity.SysRole;
import com.cy.pj.sys.service.SysRoleService;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;

    @Override
    public PageObject findPageObjects(String name, Integer pageCurrent) {
        //1.对参数进行校验
        if(pageCurrent==null || pageCurrent<1)
            throw new IllegalArgumentException("当前页码值无效");
        //2.查询总记录数并进行校验
        int rowCount=sysRoleDao.getRowCount(name);
        if(rowCount==0)
            throw new ServiceException("没有找到对应记录");
        //3.查询当前页记录
        int pageSize=4;
        int startIndex=(pageCurrent-1)*pageSize;
        List<SysRole> records=sysRoleDao.findPageObjects(name,startIndex,pageSize);
        //4.对查询结果进行封装并返回
        PageObject pageObject=new PageObject<>();
        pageObject.setPageCurrent(pageCurrent);
        pageObject.setPageSize(pageSize);
        pageObject.setRowCount(rowCount);
        pageObject.setRecords(records);
        int pageCount=rowCount/pageSize;
        if (rowCount%pageSize!=0)pageCount++;
        pageObject.setPageCount(pageCount);
        return pageObject;

    }
    @Transactional//事务切入点方法
    @Override
    public int saveObject(SysRole entity, Integer[] menuIds) {
        //1.参数有效性校验
        if(entity==null)
            throw new IllegalArgumentException("保存对象不能为空");
        if(StringUtils.isEmpty(entity.getName()))
            throw new IllegalArgumentException("角色名不允许为空");
        if(menuIds==null||menuIds.length==0)
            throw new ServiceException("必须为角色分配权限");
        //2.保存角色自身信息
        int rows=sysRoleDao.insertObject(entity);
        //3.保存角色菜单关系数据
        sysRoleDao.insertObjects(entity.getId(), menuIds);
        //4.返回业务结果
        return rows;

    }

    @Override
    public SysRole findObjectById(Integer id) {
        //验证合法性
        if(id==null || id<=0)
            throw new IllegalArgumentException("id值不符合");
        //执行查询
        SysRole result=sysRoleDao.updateObjectById(id);
        //验证结果并返回
        if (result==null)
            throw new ServiceException("此记录不存在");
        return result;
    }

    @Override
    public int updateObject(SysRole entity, Integer[] menuIds) {
        //验证合法性
        if (entity==null)
            throw new IllegalArgumentException("更新对象不能为空");
        if (entity.getId()==null)
            throw new IllegalArgumentException("id不能为空");
        if (StringUtils.isEmpty(entity.getName()))
            throw new IllegalArgumentException("角色名不能为空");
        if (menuIds==null || menuIds.length==0)
            throw new IllegalArgumentException("必须指定一个权限");
        //更新数据
        int rows=sysRoleDao.updateObject(entity);
        if (rows==0)
            throw new IllegalArgumentException("该数据已不存在");
            sysRoleMenuDao.deleteObjectsByMenuId(entity.getId());
        //返回结果
        return rows;
    }

    @Override
    public int deleteObject(Integer id) {
        //验证数据合法性
        if (id==null || id<=0)
            throw new IllegalArgumentException("请先选择");
        //基于id删除关系
        sysRoleMenuDao.deleteObjectsByRoleId(id);
        //删除角色
        int rows=sysRoleDao.deleteObject(id);
        if (rows==0)
            throw new ServiceException("此记录已不存在");
        //返回结果
        return rows;
    }


    @Override
    public List<CheckBox> findObjects() {
        return sysRoleDao.findObjects();
    }

}
